package appointmentManagement;

import entity.Appointment;
import entity.Patient;
import entity.Staff;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class AppointmentSession implements AppointmentSessionRemote {

    @PersistenceContext
    private EntityManager entityManager;

    public AppointmentSession() {
    }

    @Override
    public List<Appointment> getAllAppointments() {
        Query query = entityManager.createQuery("SELECT a FROM Appointment a");
        return query.getResultList();
    }

    @Override
    public Appointment getIdAppointment(Long appointmentId) {
        Appointment ap = entityManager.find(Appointment.class,appointmentId);
        return ap;
    }

    private boolean checkAppointmentOverwhelmed(String appointmentDepartment, Timestamp appointmentTime) {
        Query query = entityManager.createQuery("SELECT a FROM Appointment a WHERE a.appointmentDepartment = :appointmentDepartment AND a.appointmentTime= :appointmentTime");
        query.setParameter("appointmentDepartment", appointmentDepartment);
        query.setParameter("appointmentTime", appointmentTime);
        List resultList = query.getResultList();
        if (resultList.size() < 10) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Long addNewAppointment(String patientName, String citizenship, String patientIdentification,
            String appointmentDepartment, Timestamp appointmentTime, String patientContact, Patient patient, Staff doctor)
            throws AppointmentOverwhelmedException {
        if (!checkAppointmentOverwhelmed(appointmentDepartment, appointmentTime)) {
            Appointment appointment = new Appointment();
            appointment.setPatientName(patientName);
            appointment.setCitizenship(citizenship);
            appointment.setPatientIdentification(patientIdentification);
            appointment.setAppointmentDepartment(appointmentDepartment);
            appointment.setAppointmentTime(appointmentTime);
            appointment.setPatientContact(patientContact);
            appointment.setPatient(patient);
            appointment.setDoctor(doctor);
            if (doctor != null) {
                appointment.setDoctorName(doctor.getFirstName() + " " + doctor.getLastName() + "(" + doctor.getStaffID() + ")");
            }
            entityManager.persist(appointment);
            entityManager.flush();
            if (patient != null) {
                Patient p = entityManager.find(Patient.class, patient.getPin());
                List<Appointment> al = p.getAppointments();
                al.add(appointment);
                p.setAppointments(al);
                entityManager.persist(p);
                entityManager.flush();
            }
            return appointment.getAppointmentId();
        } else {
            System.out.println("Fail to Add New Appointment");
            throw new AppointmentOverwhelmedException("Appointment Overwhelmed: " + appointmentDepartment + " at " + appointmentTime);
        }
    }

    @Override
    public void updateAppointment(Long appointmentId, String patientName, String citizenship, String patientIdentification,
            String appointmentDepartment, Timestamp appointmentTime, String patientContact, Staff doctor) {
        Appointment myAppointment = entityManager.find(Appointment.class, appointmentId);
        myAppointment.setPatientName(patientName);
        myAppointment.setCitizenship(citizenship);
        myAppointment.setPatientIdentification(patientIdentification);
        myAppointment.setAppointmentDepartment(appointmentDepartment);
        myAppointment.setAppointmentTime(appointmentTime);
        myAppointment.setPatientContact(patientContact);
        if (doctor != null) {
            myAppointment.setDoctor(doctor);
            myAppointment.setDoctorName(doctor.getFirstName() + " " + doctor.getLastName() + "(" + doctor.getStaffID() + ")");
        }
        entityManager.flush();
    }

    @Override
    public void deleteAppointment(Long appointmentId) {
        Appointment myAppointment = entityManager.find(Appointment.class, appointmentId);
        Patient p = myAppointment.getPatient();
        if (p != null) {
            Patient mp = entityManager.find(Patient.class, p.getPin());
            List<Appointment> al = mp.getAppointments();
            al.remove(myAppointment);
            mp.setAppointments(al);
            entityManager.flush();
        }
        entityManager.remove(myAppointment);
        entityManager.flush();
    }
}
